//
//  RPNcalcBRAIN.m
//  RPNcalc
//
//  Created by iD Student on 6/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RPNcalcBRAIN.h"
@interface RPNcalcBRAIN ()

@property (nonatomic, strong) NSMutableArray * operandStack;

@end

@implementation RPNcalcBRAIN
@synthesize operandStack = _operandStack;

-(NSMutableArray *) operandStack
{
    if(_operandStack == nil)
    {
        _operandStack = [[NSMutableArray alloc] init];
    }
    return _operandStack;
}


-(void)pushOperand: (double)operand
{
    [self.operandStack addObject:[NSNumber numberWithDouble:operand]];
}

-(double) popOperand
{
    NSNumber * operandOpject = [self.operandStack lastObject];
    if(operandOpject)
    {
        [self.operandStack removeLastObject];
    }
    return [operandOpject doubleValue];
}

-(double) performeOperation:(NSString *)operation
{
    double result = 0;
    
    if( [operation isEqualToString:@"+"])
    {
        result = [self popOperand] + [self popOperand];
    
    }
    else if( [operation isEqualToString:@"-"])
    {
        double temp = [self popOperand];
        result = [self popOperand] - temp;
        
    }
    else if( [operation isEqualToString:@"÷"])
    {
        double temp = [self popOperand];
        result = [self popOperand] / temp;
        
    }
    else if( [operation isEqualToString:@"X"])
    {
        double temp = [self popOperand];
        result = [self popOperand] * temp;
        
    }
    else if( [operation isEqualToString:@"^2"])
    {
        double temp = [self popOperand];
        result = temp * temp;
        
    }
    else if( [operation isEqualToString:@"π"])
    {
        
        result = 3.14159265 ;
        
    }
    else if( [operation isEqualToString:@"random"])
    {
        int temp = arc4random() % 8;
        result = (double)temp ;
        
    }



    return result;
}

@end
